/**
 * 
 */
package com.noisyair.android.alvin.apps.sms;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.noisyair.android.alvin.activities.AlvinActivity;

public class SendSMSActivity extends AlvinActivity {

	String mToName;
	String mToNumber;
	String mMessage;
	SmsManager mSMSManager = SmsManager.getDefault();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.sendmessagewindow);

		// Set this View's fields from the intent that was passed in
		Intent i = getIntent();
		if (i != null) {
			((EditText) findViewById(R.id.tonameedit)).setText(i
					.getStringExtra("name"));
			((EditText) findViewById(R.id.tonumberedit)).setText(i
					.getStringExtra("number"));
			((EditText) findViewById(R.id.messageedit)).setText(i
					.getStringExtra("message"));
		}

		Button sendButton = (Button) findViewById(R.id.sendsmsbutton);
		sendButton.setOnClickListener(mClickListener);
	}

	private OnClickListener mClickListener = new OnClickListener() {

		public void onClick(View v) {
			if (v == findViewById(R.id.sendsmsbutton)) {
				prepareSMSAndSend();
			}
		}
	};

	private void prepareSMSAndSend() {
		mToName = ((EditText) findViewById(R.id.tonameedit)).getText()
				.toString();
		mToNumber = ((EditText) findViewById(R.id.tonumberedit)).getText()
				.toString();
		mMessage = ((EditText) findViewById(R.id.messageedit)).getText()
				.toString();
		Log.i("Alvin", "message: " + mMessage + "\nTo: " + mToName + "\nNum: "
				+ mToNumber);

		mSMSManager.sendTextMessage(mToNumber, null, mMessage, null, null);
		finish();
	}

	/* Creates the menu items */
	private static final int GET_CONTACT_MENU_ID = 1004;
    static final int PICK_CONTACT_SMS_REQUEST = 9999;
    
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, GET_CONTACT_MENU_ID, 0, "Select Contact");
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case GET_CONTACT_MENU_ID:
			Intent i = new Intent();
			i.setClassName("com.noisyair.android.alvin.apps.contacts", "com.noisyair.android.alvin.apps.contacts.AlvinContactPicker");
            i.putExtra("wantsSMS", "yes");
			startActivityForResult(i,PICK_CONTACT_SMS_REQUEST);
			return true;
		}
		return false;
	}
	
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == PICK_CONTACT_SMS_REQUEST) {
            if (resultCode == RESULT_OK) {
            	
            	Bundle extras = data.getExtras();
    			((EditText) findViewById(R.id.tonameedit)).setText(extras
    					.getString("name"));
    			((EditText) findViewById(R.id.tonumberedit)).setText(extras
    					.getString("number"));
    					
    			((EditText) findViewById(R.id.messageedit)).setText("");          	
            }
        }
    }
}
